﻿#region

using System;
using System.ServiceModel;
using System.Windows.Browser;
using YouTubePlaylist.MailService;
using YouTubePlaylist.Models;
using YouTubePlaylist.MusicVideoService;

#endregion

namespace YouTubePlaylist.Libraries
{
    public static class MailClient
    {
        public static void SendMail(string from, string to, string subject, string body)
        {
            var _client =
                new MailServiceClient(
                    new BasicHttpBinding {MaxBufferSize = 2147483647, MaxReceivedMessageSize = 2147483647},
                    new EndpointAddress(DataLayer.EndPoint + "MailService.svc"));
            _client.SendMailAsync(from, to, subject, body);
        }

        public static string GetLink(VideoPlaylist p)
        {
            return GetLink(p, null);
        }

        public static string GetLink(VideoPlaylist p, VideoTrack t)
        {
            Uri url = HtmlPage.Document.DocumentUri;
            string address = url.AbsoluteUri;
            if (!string.IsNullOrEmpty(url.Query))
            {
                address = address.Substring(0, address.IndexOf(url.Query));
            }
            User u = DataLayer.Singleton.User;
            return t == null
                       ? string.Format("{0}?user={1}&playlist={2}", address, u.ExternalKey, p.Id)
                       : string.Format("{0}?user={1}&playlist={2}&track={3}", address, u.ExternalKey, p.Id, t.Id);
        }
    }
}